#include "skscene.h"
#include <QGraphicsSceneMouseEvent>
#include <QDebug>
#include "skpoint.h"
#include "skline.h"
#include "skcircle.h"
#include "sksegment.h"

SkScene::SkScene(QObject * parent):QGraphicsScene(parent)
{
    curState = skNormal;
    skParent = new QGraphicsWidget();
    this->addItem(skParent);

    //dodajmy jakies primitivy osie X i Y - to mogloby byc na backgorund
    QLineF osX(0.0,0.0,50.0,0.0);
    QLineF osY(0.0,0.0,0.0,50.0);
    QGraphicsLineItem* l1= new QGraphicsLineItem();
    QGraphicsLineItem* l2= new QGraphicsLineItem();
    l1->setZValue(-5);
    l2->setZValue(-5);
    l1->setLine(osX);
    l2->setLine(osY);
    l1->setPen(QPen(Qt::green, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
    l2->setPen(QPen(Qt::darkBlue, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
    this->addItem(l1);
    this->addItem(l2);
}
void SkScene::setSketchModel(SketchModelWrapper* mod){
	model = mod;
}

QGraphicsWidget* SkScene::getSkParent(){
    return skParent;
}
void SkScene::drawLine(){
    curState = skDrawLine;
    clickedPoints.clear();
}
void SkScene::drawCircle(){
    curState = skDrawCircle;
    clickedPoints.clear();
}
void SkScene::drawSegment(){
    curState = skDrawSegment;
    clickedPoints.clear();
}

void SkScene::setNormal(){
    curState = skNormal;
    clickedPoints.clear();
}
void SkScene::addSkPoint(SkPoint* point){
    this->addItem(point);
}
void SkScene::addSkLine(SkLine* line){
    this->addItem(line);
    this->addSkPoint(line->sourcePoint());
    this->addSkPoint(line->destPoint());
}
void SkScene::addSkCircle(SkCircle* cir){
    this->addItem(cir);
    this->addSkPoint(cir->sourcePoint());
    this->addSkPoint(cir->destPoint());
}
void SkScene::addSkSegment(SkSegment* segment){
	Q_UNUSED(segment);

}
void SkScene::pointPosChanged(int i, QPointF newPos){

    emit pointPosHasChanged(i,newPos);
}
void SkScene::pointClicked(int i){
    emit pointHasClicked(i);
}

void SkScene::mousePressEvent( QGraphicsSceneMouseEvent * mouseEvent ){


    if(curState!=skNormal){

        if(mouseEvent->button()==Qt::LeftButton){
            clickedPoints << mouseEvent->scenePos();
        }

        if(mouseEvent->button()==Qt::RightButton){
            //konczymy zbieranie punktow

            switch(curState){
                // tu bedzie emitowanie sygnalu do modelu
                case skDrawLine:
                    if(clickedPoints.size()==2){

//                        SkPoint* n1 = new SkPoint(1,clickedPoints.at(0),6,skParent);
//                        SkPoint* n2 = new SkPoint(2,clickedPoints.at(1),6,skParent);
//                        SkLine* skline = new SkLine(1,n1,n2,skParent);
                        //addSkLine(skline);
                        model->addLine(Coordinate(clickedPoints.at(0).x(),clickedPoints.at(0).y()),Coordinate(clickedPoints.at(1).x(),clickedPoints.at(1).y()));

                    }
                break;

                case skDrawCircle:
                    if(clickedPoints.size()==2){

//                        SkPoint* n1 = new SkPoint(1,clickedPoints.at(0),6,skParent);
//                        SkPoint* n2 = new SkPoint(2,clickedPoints.at(1),6,skParent);
//                        SkCircle* skcircle = new SkCircle(1,n1,n2,skParent);
                        model->addCircle(Coordinate(clickedPoints.at(0).x(),clickedPoints.at(0).y()),Coordinate(clickedPoints.at(1).x(),clickedPoints.at(1).y()));
                    }
                break;
                case skDrawSegment:
                if(clickedPoints.size()>2){
//                    SkSegment* segment = new SkSegment(1,clickedPoints,skParent);
                    std::list<Coordinate> pnt;
                    for(int i=0;i<clickedPoints.size();i++){
                    	pnt.push_back(Coordinate(clickedPoints.at(i).x(),clickedPoints.at(i).y()));
                    }
                    model->addSegment(pnt);

                }
                break;
                case skNormal:
                	break;
                case skSelectPointsForConstraint:
                	break;
            }

            clickedPoints.clear();
            setNormal();
            update();
        }
    }


    QGraphicsScene::mousePressEvent( mouseEvent );

}
void SkScene::clearScene(){
    this->removeItem(skParent);
    delete skParent;
    skParent = new QGraphicsWidget();
    this->addItem(skParent);
}
